<template>
  <div class="question-board-card">
    <div class="board-header">
      <h2>问题栏</h2>
      <button class="add-btn" @click="showAddForm = true">+ 添加问题</button>
    </div>
    
    <div v-if="showAddForm" class="add-form">
      <input 
        v-model="newQuestion.text" 
        placeholder="输入你的问题..." 
        class="question-input"
        @keyup.enter="addQuestion"
      />
      <select v-model="newQuestion.subject" class="subject-select">
        <option value="">选择科目</option>
        <option value="数学">数学</option>
        <option value="英语">英语</option>
        <option value="政治">政治</option>
        <option value="专业课">专业课</option>
      </select>
      <div class="form-actions">
        <button @click="addQuestion" class="save-btn">保存</button>
        <button @click="cancelAdd" class="cancel-btn">取消</button>
      </div>
    </div>

    <div class="questions-list">
      <div 
        v-for="(question, index) in questions" 
        :key="index" 
        class="question-item"
        :class="{ solved: question.solved }"
      >
        <div class="question-content">
          <div class="question-text">{{ question.text }}</div>
          <div class="question-meta">
            <span class="subject-tag">{{ question.subject }}</span>
            <span class="date">{{ question.date }}</span>
          </div>
        </div>
        <div class="question-actions">
          <button 
            @click="toggleSolved(index)" 
            class="solve-btn"
            :class="{ solved: question.solved }"
          >
            {{ question.solved ? '✓ 已解决' : '标记解决' }}
          </button>
          <button @click="deleteQuestion(index)" class="delete-btn">删除</button>
        </div>
      </div>
    </div>

    <div v-if="questions.length === 0" class="empty-state">
      还没有问题，开始记录你的疑问吧！
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const defaultQuestions = [
  {
    text: '高数中泰勒展开式的推导过程不太理解',
    subject: '数学',
    date: '2024-01-15',
    solved: false
  },
  {
    text: '英语阅读理解中主旨题的技巧',
    subject: '英语',
    date: '2024-01-14',
    solved: true
  }
]

const questions = ref([])
const showAddForm = ref(false)
const newQuestion = ref({
  text: '',
  subject: ''
})

// 从localStorage加载数据
const loadQuestions = () => {
  const saved = localStorage.getItem('questions')
  if (saved) {
    try {
      questions.value = JSON.parse(saved)
    } catch (e) {
      console.error('加载问题数据失败:', e)
      questions.value = [...defaultQuestions]
    }
  } else {
    questions.value = [...defaultQuestions]
  }
}

// 保存到localStorage
const saveQuestions = () => {
  localStorage.setItem('questions', JSON.stringify(questions.value))
}

// 监听数据变化并保存
watch(questions, () => {
  saveQuestions()
}, { deep: true })

const addQuestion = () => {
  if (newQuestion.value.text.trim() && newQuestion.value.subject) {
    questions.value.unshift({
      text: newQuestion.value.text,
      subject: newQuestion.value.subject,
      date: new Date().toISOString().split('T')[0],
      solved: false
    })
    newQuestion.value = { text: '', subject: '' }
    showAddForm.value = false
  }
}

const cancelAdd = () => {
  newQuestion.value = { text: '', subject: '' }
  showAddForm.value = false
}

const toggleSolved = (index) => {
  questions.value[index].solved = !questions.value[index].solved
}

const deleteQuestion = (index) => {
  questions.value.splice(index, 1)
}

// 初始化加载
loadQuestions()
</script>

<style scoped>
.question-board-card {
  background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 100%);
  border-radius: 20px;
  padding: 30px;
  color: white;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.board-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.board-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.add-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 25px;
  cursor: pointer;
  font-size: 14px;
  transition: transform 0.3s ease;
}

.add-btn:hover {
  transform: translateY(-2px);
}

.add-form {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 20px;
  margin-bottom: 20px;
  backdrop-filter: blur(10px);
}

.question-input {
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 12px;
  color: white;
  margin-bottom: 10px;
  font-size: 14px;
}

.subject-select {
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 12px;
  color: white;
  margin-bottom: 10px;
  font-size: 14px;
}

.subject-select option {
  background: #1a1a2e;
  color: white;
}

.form-actions {
  display: flex;
  gap: 10px;
}

.save-btn, .cancel-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.save-btn {
  background: #4caf50;
  color: white;
}

.cancel-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
}

.questions-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.question-item {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 20px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.question-item:hover {
  background: rgba(255, 255, 255, 0.15);
}

.question-item.solved {
  opacity: 0.7;
  background: rgba(76, 175, 80, 0.1);
}

.question-content {
  margin-bottom: 15px;
}

.question-text {
  font-size: 16px;
  margin-bottom: 8px;
  line-height: 1.4;
}

.question-meta {
  display: flex;
  gap: 10px;
  align-items: center;
}

.subject-tag {
  background: rgba(100, 255, 218, 0.2);
  color: #64ffda;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.date {
  color: rgba(255, 255, 255, 0.6);
  font-size: 12px;
}

.question-actions {
  display: flex;
  gap: 10px;
}

.solve-btn, .delete-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s ease;
}

.solve-btn {
  background: rgba(100, 255, 218, 0.2);
  color: #64ffda;
  border: 1px solid rgba(100, 255, 218, 0.3);
}

.solve-btn.solved {
  background: #4caf50;
  color: white;
}

.delete-btn {
  background: rgba(244, 67, 54, 0.2);
  color: #f44336;
  border: 1px solid rgba(244, 67, 54, 0.3);
}

.empty-state {
  text-align: center;
  color: rgba(255, 255, 255, 0.6);
  padding: 40px;
  font-style: italic;
}
</style>